#include "SystemSettingsDlg.h"

#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QComboBox>
#include <QRadioButton>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QFileDialog>
#include "VisionMainWnd.h"
#include "ICamera.h"
#include "InspectLL.h"
//#include "IInfer.h"

SystemSettingsDlg::SystemSettingsDlg(QWidget* parent)
{
	setWindowTitle("系统设置");
	setParent(parent, windowFlags());
	setWindowFlags(windowFlags() | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);

	initUI();
}

SystemSettingsDlg::~SystemSettingsDlg()
{
}

bool SystemSettingsDlg::initUI()
{
	QWidget* mainWidget = new QWidget(this);
	// 系统相关设置
	QGroupBox* systemGroup = createSysSetGroupBox(mainWidget);

	// 相机参数组
	QGroupBox* camGroup = createCamGroupBox(mainWidget);

	// 调试信息参数
	QGroupBox* debugGroup = createDebugGroupBox(mainWidget);

	QHBoxLayout* hBoxMainLyt = new QHBoxLayout();
	hBoxMainLyt->addWidget(systemGroup);
	hBoxMainLyt->addWidget(camGroup);
	hBoxMainLyt->addWidget(debugGroup);

	QGroupBox* aiGroup = createAIGroupBox(mainWidget);

	QVBoxLayout* mainLayout = new QVBoxLayout();
	mainLayout->addLayout(hBoxMainLyt);
	mainLayout->addWidget(aiGroup);

	setLayout(mainLayout);
	return false;
}

void SystemSettingsDlg::initCamParam()
{

}

QGroupBox* SystemSettingsDlg::createCamGroupBox(QWidget *parent)
{
	// 相机设置
	QGroupBox* camGroup = new QGroupBox("相机设置", parent);

	QVBoxLayout* groupLyt = new QVBoxLayout();

	QStringList trigMode = { "Off", "On" };
	QHBoxLayout* hLytCamTrigMode = nullptr;
	QComboBox *camTrigModeBox = createlineComboxComb("外触发:", trigMode, hLytCamTrigMode);
	camTrigModeBox->setObjectName("trigModeComb");
	groupLyt->addLayout(hLytCamTrigMode);
	connect(camTrigModeBox, &QComboBox::currentIndexChanged, this, [=](int index)
		{
			VisionMainWnd* p = (VisionMainWnd*)parentWidget();
			p->getCamera()->SetTriggerMode(index);
		});

	QStringList trigSrcs = { "SoftWare", "Line1", "Line2", "Line3", "Line4" };
	QHBoxLayout* hLytCamTrigSrc = nullptr;
	QComboBox* comboxTrigSrcs = createlineComboxComb("触发源:", trigSrcs, hLytCamTrigSrc);
	comboxTrigSrcs->setObjectName("trigSrcComb");
	groupLyt->addLayout(hLytCamTrigSrc);
	connect(comboxTrigSrcs, &QComboBox::currentIndexChanged, this, [=](int index)
		{
			XCamera::TriggerSource trigSrc = XCamera::TriggerSource(index);
			VisionMainWnd* p = (VisionMainWnd*)parentWidget();
			p->getCamera()->SetTriggerSource(trigSrc);
		});

	QHBoxLayout* hLytExposure = nullptr;
	QLineEdit* line = createLineEditComb("曝光时间:", "us", hLytExposure);
	line->setText("500");
	line->setObjectName("expTimeEdit");
	groupLyt->addLayout(hLytExposure);
	connect(line, &QLineEdit::editingFinished, this, [=]
		{
			int expTime = line->text().toInt();
			VisionMainWnd* p = (VisionMainWnd*)parentWidget();
			p->getCamera()->SetExposureTime(expTime);
		});

	QHBoxLayout* hLytGain = nullptr;
	QLineEdit* editGain = createLineEditComb("增益:", "DN", hLytGain);
	editGain->setText("5");
	editGain->setObjectName("gainEdit");
	groupLyt->addLayout(hLytGain);	
	connect(editGain, &QLineEdit::editingFinished, this, [=]
		{
			int gain = editGain->text().toInt();
			VisionMainWnd* p = (VisionMainWnd*)parentWidget();
			p->getCamera()->SetExposureTime(gain);
		});

	QHBoxLayout* hLytFrmRate = nullptr;
	QLineEdit* editFrmRate = createLineEditComb("帧率:", "", hLytFrmRate);
	editFrmRate->setText("5");
	editFrmRate->setObjectName("gainEdit");
	groupLyt->addLayout(hLytFrmRate);
	connect(editFrmRate, &QLineEdit::editingFinished, this, [=]
		{
			double fps = editFrmRate->text().toDouble();
			VisionMainWnd* p = (VisionMainWnd*)parentWidget();
			p->setGrabFrames(fps);	// 针对软触发方式进行设置帧率
			//p->getCamera()->SetFrameRate(fps);	// 该方式针对trigMode为"Off"的情况
		});

	camGroup->setLayout(groupLyt);

	return camGroup;
}

QGroupBox* SystemSettingsDlg::createSysSetGroupBox(QWidget* parent)
{
	QGroupBox* stopPLineGroupBox = new QGroupBox("产线停机条件", parent);
	QCheckBox* chkEnableStopPLine = new QCheckBox("允许停机");
	QCheckBox* chkRobotDisConn = new QCheckBox("机器人断开");
	QCheckBox* chkCamDisConn = new QCheckBox("相机断开");

	QVBoxLayout* mainLyt = new QVBoxLayout();
	mainLyt->addWidget(chkEnableStopPLine);
	mainLyt->addWidget(chkRobotDisConn);
	mainLyt->addWidget(chkCamDisConn);
	mainLyt->addStretch();

	stopPLineGroupBox->setLayout(mainLyt);
	

	return stopPLineGroupBox;
}

QGroupBox* SystemSettingsDlg::createDebugGroupBox(QWidget* parent)
{

	QGroupBox* debugParamsGroupBox = new QGroupBox("调试参数", parent);
	QCheckBox* chkShowRoi = new QCheckBox("显示ROI");
	QCheckBox* chkShowScore = new QCheckBox("显示分数");
	QCheckBox* chkShowAllObj = new QCheckBox("所有目标");
	QCheckBox* chkSaveSrcImage = new QCheckBox("保存图像");
	QCheckBox* chkMatchDetails = new QCheckBox("识别详细信息");	// 显示所有匹配结果的列表

	QVBoxLayout* mainLyt = new QVBoxLayout();
	mainLyt->addWidget(chkShowRoi);
	mainLyt->addWidget(chkShowScore);
	mainLyt->addWidget(chkShowAllObj);
	mainLyt->addWidget(chkSaveSrcImage);
	mainLyt->addWidget(chkMatchDetails);
	mainLyt->addStretch();

	debugParamsGroupBox->setLayout(mainLyt);

	// 初始化状态

	VisionMainWnd* p = (VisionMainWnd*)parentWidget();
	InspectLL* insp = p->getInspect();

	auto item = p->getRoiRectItem();
	if (item)
	{
		chkShowRoi->setChecked(item->isVisible());		
	}

	chkSaveSrcImage->setChecked(insp->isSaveSrcImage());
	chkShowAllObj->setChecked(insp->isDrawAllResult());
	
	connect(chkShowRoi, &QCheckBox::stateChanged, this, [p](int stat) {
		p->showRoiRect(stat == Qt::Checked ? true : false);
		});
	connect(chkSaveSrcImage, &QCheckBox::stateChanged, this, [insp](int stat) {
		insp->setSaveSrcImage(stat == Qt::Checked ? true : false);
		});
	connect(chkShowAllObj, &QCheckBox::stateChanged, [insp](int stat) {
		insp->setDrawAllResult(stat == Qt::Checked ? true : false);
		});
	connect(chkMatchDetails, &QCheckBox::stateChanged, this, [p](int stat) {

		});

	

	return debugParamsGroupBox;
}

QGroupBox* SystemSettingsDlg::createAIGroupBox(QWidget* parent)
{
	QGroupBox* paramsGroupBox = new QGroupBox("参数设置");
	// 参数设置
	QLabel* labConfidence = new QLabel("置信度:");
	QLineEdit* editConfidence = new QLineEdit();
	QLabel* labScore = new QLabel("分数:");
	QLineEdit* editScore = new QLineEdit();
	QLabel* labNms = new QLabel("NMS:");
	QLineEdit* editNms = new QLineEdit();
	QPushButton* btnSetParams = new QPushButton("设置");
	editConfidence->setText("0.25");
	editScore->setText("0.25");
	editNms->setText("0.45");

	QHBoxLayout* paramsBox = new QHBoxLayout();
	paramsBox->addWidget(labConfidence);
	paramsBox->addWidget(editConfidence);
	paramsBox->addWidget(labScore);
	paramsBox->addWidget(editScore);
	paramsBox->addWidget(labNms);
	paramsBox->addWidget(editNms);
	paramsBox->addWidget(btnSetParams);

	paramsGroupBox->setLayout(paramsBox);

	// 模型和类别文件加载
	QLineEdit* modelFilePath = new QLineEdit();
	QPushButton* modelBtnLoad = new QPushButton("加载模型");
	QLineEdit* classesFilePath = new QLineEdit();
	QPushButton* classesBtnLoad = new QPushButton("加载类别文件");
	QGridLayout* gridLyt = new QGridLayout();
	gridLyt->addWidget(modelFilePath, 0, 0, 1, 1);
	gridLyt->addWidget(modelBtnLoad, 0, 1, 1, 1);
	gridLyt->addWidget(classesFilePath, 1, 0, 1, 1);
	gridLyt->addWidget(classesBtnLoad, 1, 1, 1, 1);

	// gpu，intel集显，cpu三种推理
	//QRadioButton* gpuCheck = new QRadioButton("GPU-Infer");
	//QRadioButton* intelGpuCheck = new QRadioButton("IntelGpu-Infer");
	//QRadioButton* cpuCheck = new QRadioButton("CPU-Infer");

	QVBoxLayout* mainLyt = new QVBoxLayout();
	mainLyt->addWidget(paramsGroupBox);
	mainLyt->addLayout(gridLyt);
	QGroupBox* mainGroupBox = new QGroupBox("AI", parent);
	mainGroupBox->setLayout(mainLyt);

	VisionMainWnd* p = (VisionMainWnd*)parentWidget();
	//IInfer* infer = p->getInferPtr();
	connect(btnSetParams, &QPushButton::clicked, [=] {
		double confidenceThresh = editConfidence->text().toDouble();
		double scoreThresh = editScore->text().toDouble();
		double nmsThresh = editNms->text().toDouble();
		/*if (infer)
		{
			infer->setParams(confidenceThresh, scoreThresh, nmsThresh);
		}*/
		});
	connect(modelBtnLoad, &QPushButton::clicked, [=] {
		QString modelFile = QFileDialog::getOpenFileName(this, "加载模型文件", "", "Model Files(*.onnx *.pt);;All(*.*)");
		/*if (infer && !modelFile.isEmpty())
		{
			infer->loadModelFile(modelFile.toUtf8().constData());
			modelFilePath->setText(modelFile);
		}
		else
		{
			qInfo() << "加载模型文件失败。";
		}*/
		});
	connect(classesBtnLoad, &QPushButton::clicked, [=] {
		QString classesFile = QFileDialog::getOpenFileName(this, "加载目标类别文件", "", "Model Files(*.txt *.yaml);;All(*.*)");
		/*if (infer && !classesFile.isEmpty())
		{
			infer->loadLabelFile(classesFile.toUtf8().constData());
			classesFilePath->setText(classesFile);
		}
		else
		{
			qInfo() << "加载目标类别文件失败。";
		}*/
		});

	return mainGroupBox;
}

QLineEdit* SystemSettingsDlg::createLineEditComb(QString lab, QString lab2, QHBoxLayout*&hLyt)
{
	hLyt = new QHBoxLayout();
	QLabel* label = new QLabel(lab);
	QLineEdit* lineEdit = new QLineEdit();
	hLyt->addWidget(label);
	hLyt->addWidget(lineEdit);
	if (!lab2.isEmpty())
	{
		QLabel* label2 = new QLabel(lab2);
		hLyt->addWidget(label2);
	}
	return lineEdit;
}

QComboBox* SystemSettingsDlg::createlineComboxComb(QString lab, QStringList comboxItems, QHBoxLayout*& hLyt)
{
	QLabel* label = new QLabel(lab);
	QComboBox* combox = new QComboBox();
	if (!comboxItems.isEmpty())
	{
		combox->addItems(comboxItems);
	}

	hLyt = new QHBoxLayout();
	hLyt->addWidget(label);
	hLyt->addWidget(combox);

	return combox;
}